package com.example.dw.admin.controller;

import com.example.dw.common.Result;
import com.example.dw.common.ResultCode;
import com.example.dw.admin.dto.PageRequestDTO;
import com.example.dw.admin.dto.CreateActivityDTO;
import com.example.dw.admin.dto.UpdateActivityDTO;
import com.example.dw.admin.service.AdminActivityService;
import com.example.dw.entity.Activity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 管理端活动管理控制器
 * @author system
 * @since 2024-01-01
 */
@Tag(name = "管理端活动管理", description = "活动信息管理接口")
@Slf4j
@RestController
@RequestMapping("/admin")
@Validated
public class AdminActivityController {

    @Autowired
    private AdminActivityService adminActivityService;

    /**
     * 获取部分活动
     */
    @Operation(summary = "获取部分活动", description = "获取指定数量的活动")
    @GetMapping("/activitySome/{count}")
    public Result<?> getSomeActivity(@PathVariable Integer count) {
        try {
            List<Activity> activities = adminActivityService.getSomeActivity(count);
            return Result.success("获取活动列表成功", activities);
        } catch (Exception e) {
            log.error("获取部分活动失败: {}", e.getMessage());
            return Result.error("获取活动列表失败");
        }
    }

    /**
     * 分页获取活动信息
     */
    @Operation(summary = "分页获取活动信息", description = "分页查询活动信息")
    @PostMapping("/activity/page")
    public Result<?> getActivityByPage(@RequestBody @Validated PageRequestDTO pageRequest) {
        try {
            Map<String, Object> result = adminActivityService.getActivityByPage(pageRequest);
            return Result.success("获取活动列表成功", result);
        } catch (Exception e) {
            log.error("分页获取活动信息失败: {}", e.getMessage());
            return Result.error("获取活动列表失败");
        }
    }

    /**
     * 获取活动信息
     */
    @Operation(summary = "获取活动信息", description = "根据活动ID获取活动详细信息")
    @GetMapping("/activityY/{activityid}")
    public Result<?> getActivity(@PathVariable Integer activityid) {
        try {
            Activity activity = adminActivityService.getActivityById(activityid);
            return Result.success("获取活动信息成功", activity);
        } catch (Exception e) {
            log.error("获取活动信息失败, activityId: {}, 错误: {}", activityid, e.getMessage());
            return Result.error("获取活动信息失败");
        }
    }

    /**
     * 创建活动
     */
    @Operation(summary = "创建活动", description = "创建新的活动")
    @PostMapping("/activity")
    public Result<?> createActivity(@RequestBody @Validated CreateActivityDTO createActivityDTO, 
                                  HttpServletRequest request) {
        try {
            Integer adminId = (Integer) request.getAttribute("adminId");
            createActivityDTO.setCreatorId(adminId);
            
            adminActivityService.createActivity(createActivityDTO);
            return Result.success("创建活动成功");
        } catch (Exception e) {
            log.error("创建活动失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 更新活动信息
     */
    @Operation(summary = "更新活动信息", description = "更新活动的基本信息")
    @PutMapping("/activity")
    public Result<?> updateActivity(@RequestBody @Validated UpdateActivityDTO updateActivityDTO) {
        try {
            adminActivityService.updateActivity(updateActivityDTO);
            return Result.success("更新活动信息成功");
        } catch (Exception e) {
            log.error("更新活动信息失败: {}", e.getMessage());
            return Result.error("更新活动信息失败");
        }
    }

    /**
     * 删除活动
     */
    @Operation(summary = "删除活动", description = "删除指定的活动")
    @DeleteMapping("/activity")
    public Result<?> deleteActivity(@RequestParam Integer activityid) {
        try {
            adminActivityService.deleteActivity(activityid);
            return Result.success("删除活动成功");
        } catch (Exception e) {
            log.error("删除活动失败, activityId: {}, 错误: {}", activityid, e.getMessage());
            return Result.error("删除活动失败");
        }
    }

    /**
     * 导出活动报名信息
     */
    @Operation(summary = "导出活动报名信息", description = "导出指定活动的报名信息")
    @PostMapping("/activity/join")
    public Result<?> getActivityJoin(@RequestBody Map<String, Object> params) {
        try {
            Integer activityid = (Integer) params.get("activityid");
            List<Map<String, Object>> joinList = adminActivityService.getActivityJoin(activityid);
            return Result.success("获取活动报名信息成功", joinList);
        } catch (Exception e) {
            log.error("导出活动报名信息失败: {}", e.getMessage());
            return Result.error("导出活动报名信息失败");
        }
    }

    /**
     * 获取全部活动
     */
    @Operation(summary = "获取全部活动", description = "获取所有活动列表")
    @GetMapping("/activity/all")
    public Result<?> getActivityAll() {
        try {
            List<Activity> activities = adminActivityService.getAllActivity();
            return Result.success("获取活动列表成功", activities);
        } catch (Exception e) {
            log.error("获取全部活动失败: {}", e.getMessage());
            return Result.error("获取活动列表失败");
        }
    }

    /**
     * 获取小类和大类信息
     */
    @Operation(summary = "获取活动分类信息", description = "获取活动的大类和小类信息")
    @GetMapping("/activity/LittletypeAndBigtype")
    public Result<?> getLittletypeAndBigtype() {
        try {
            Map<String, Object> typeInfo = adminActivityService.getLittletypeAndBigtype();
            return Result.success("获取活动分类信息成功", typeInfo);
        } catch (Exception e) {
            log.error("获取活动分类信息失败: {}", e.getMessage());
            return Result.error("获取活动分类信息失败");
        }
    }

    /**
     * 删除价格信息
     */
    @Operation(summary = "删除价格信息", description = "删除活动的价格信息")
    @PostMapping("/active/deleteSessionPrice")
    public Result<?> deleteSessionPrice(@RequestBody Map<String, Object> params) {
        try {
            Integer priceId = (Integer) params.get("priceId");
            adminActivityService.deleteSessionPrice(priceId);
            return Result.success("删除价格信息成功");
        } catch (Exception e) {
            log.error("删除价格信息失败: {}", e.getMessage());
            return Result.error("删除价格信息失败");
        }
    }
} 